﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Dynamic;
using Facebook;
using System.Windows.Forms;
using System.Collections;
using System.Drawing;
using System.IO;
using System.Net;
using System.Threading;
using System.Diagnostics;

namespace JOthello.FB
{
    public static class FBHelper
    {
        private static FacebookClient _FBC;
        /// <summary>
        /// facebookclient
        /// </summary>
        public static FacebookClient FBC
        {
            get
            {
                if (_FBC == null) _FBC = new FacebookClient();
                return _FBC;
            }
            private set
            {
                _FBC = value;
            }
        }

        /// <summary>
        /// AppID
        /// </summary>
        public static String AppID
        {
            get
            {
                return Properties.Settings.Default.FBAppID;
            }
        }

        /// <summary>
        /// the facebook permissions
        /// </summary>
        public static string ExtendedPermissions = "user_about_me";

        private static Uri _LoginUri = null;
        /// <summary>
        /// loginuri
        /// </summary>
        public static Uri LoginUri
        {
            get
            {
                if (_LoginUri == null)
                {
                    dynamic parameters = new ExpandoObject();
                    parameters.client_id = AppID;
                    parameters.redirect_uri = "https://www.facebook.com/connect/login_success.html";
                    parameters.response_type = "token";
                    parameters.display = "popup";
                    parameters.scope = ExtendedPermissions;

                    _LoginUri = FBC.GetLoginUrl(parameters);
                }
                return _LoginUri;
            }
        }

        private static String _AccessToken = null;
        /// <summary>
        /// AccessToken
        /// </summary>
        public static String AccessToken
        {
            get
            {
                return _AccessToken;
            }
            set
            {
                _AccessToken = value;
                FBC = new FacebookClient(_AccessToken);
            }
        }

        private static IDictionary<string, object> _Me = null;
        /// <summary>
        /// Dictionary vom aktuell eingellogten user
        /// </summary>
        public static IDictionary<string, object> Me
        {
            get
            {
                if (_Me == null) reLoadME();
                return _Me;
            }
        }

        /// <summary>
        /// lädt das me neu
        /// </summary>
        public static void reLoadME()
        {
            _Me = (IDictionary<string, object>)FBC.Get("me");
        }

        public static String Vorname
        {
            get
            {
                return Me["first_name"].ToString();
            }
        }
        public static String Nachname
        {
            get
            {
                return Me["last_name"].ToString();
            }
        }
        public static String Fullname
        {
            get
            {
                return Vorname + " " + Nachname;
            }
        }
        private static Bitmap _Bild = null;
        public static Bitmap Bild
        {
            get
            {
                if (_Bild == null) reLoadBild();
                return _Bild;
            }
        }

        public static void reLoadBild()
        {
            _Bild = new Bitmap(new MemoryStream(new WebClient().DownloadData("https://graph.facebook.com/" + Me["id"].ToString() + "/picture?type=normal")));
        }
        private static Action BildLoadCallback = null;
        public static void reLoadBildAsync(Action Callback = null)
        {
            BildLoadCallback = Callback;
            WebClient bildDownloader = new WebClient();
            bildDownloader.DownloadDataCompleted += new DownloadDataCompletedEventHandler(bildDownloader_DownloadDataCompleted);
            bildDownloader.DownloadDataAsync(new Uri("https://graph.facebook.com/" + Me["id"].ToString() + "/picture?type=large"));
        }
        private static void bildDownloader_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
        {
            WebClient wc = sender as WebClient;
            if (wc != null)
            {
                try
                {
                    _Bild = new Bitmap(new MemoryStream(e.Result));
                }
                catch (Exception ex)
                {
                    Debug.Print(ex.Message);
                    reLoadBildAsync();
                }
                if (BildLoadCallback != null) BildLoadCallback();
            }
        }

        /// <summary>
        /// loggt den aktuellen user aus
        /// </summary>
        public static void logOut()
        {
            Uri logoutUrl = FBC.GetLogoutUrl(new
            {
                next = "https://www.facebook.com/connect/login_success.html",
                access_token = AccessToken
            });

            WebBrowser webBrowser = new WebBrowser();
            webBrowser.Navigate(logoutUrl.AbsoluteUri);
        }
    }
}
